/*
 * @(#)MaintWorkflowStepParameterAction.java
 * 
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.workflow.bean.WorkflowAction;
import com.dcivision.workflow.bean.WorkflowRecord;
import com.dcivision.workflow.core.SystemWorkflowConstant;
import com.dcivision.workflow.core.WorkflowProcessor;
import com.dcivision.workflow.dao.WorkflowActionDAObject;
import com.dcivision.workflow.dao.WorkflowRecordDAObject;

/**
 * @author brook.huang edit
 * @company DCIVision Limited
 * @creation date 2006/8/29
 * @version $Revision: 1.7 $
 */
public class MaintWorkflowStepParameterAction extends
    com.dcivision.framework.web.AbstractMaintAction {

  /**
   * getFunctionCode
   * 
   * Abstract function which sub-class should implement to return the corresponding function code.
   * 
   * @return The function code
   */
  public String getFunctionCode() {
    return null;
  }

  /**
   * getMajorDAOClassName
   * 
   * Abstract function which sub-class should implement to return the corresponding major DAO class
   * name used in this class.
   * 
   * @return The DAO class name
   */
  public String getMajorDAOClassName() {
    return null;
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws ServletException {
    MaintWorkflowStepParameterForm parameterform = (MaintWorkflowStepParameterForm) form;
    // TriggerSubWorkflow autotask=new TriggerSubWorkflow();

    WorkflowActionDAObject actionDAO = new WorkflowActionDAObject(this
        .getSessionContainer(request), this.getConnection(request));
    // log.debug(">>>>>>>>>>>"+request.getParameter("actionType"));
    parameterform.setActionType(request.getParameter("actionType"));

    // BUG:EIP-110 brook.huang_add_start
    WorkflowRecordDAObject recordDAO = new WorkflowRecordDAObject(this
        .getSessionContainer(request), this.getConnection(request));
    // NOTE: I'm not sure of what type of workflow can be triggered as sub workflow?
    // current sub workflow is limited to be General Workflow.
    Integer subWorkflowCategoryID = new Integer(SystemWorkflowConstant.SYS_GENERAL_WORKFLOW); // Sub workflow is limited to be General Workflow.
    List recordList = recordDAO.getListCanSubmit(subWorkflowCategoryID); // Use the list of submition (category name is 'General Workflow') of current user.
//    List recordList = recordDAO
//        .getListByWorkflowGroupIDWorkflowCategoryIDWorkflowRecordStatus(
//            null,
//            (((String) request.getParameter("categoryID")).equals("1")) ? (new Integer(
//                1))
//                : (new Integer(0)), GlobalConstant.RECORD_RELEASED, Utility
//                .getCurrentTimestamp().toString());
    String[][] parameterOptions = null;
    if (!Utility.isEmpty(recordList)) {
      parameterOptions = new String[recordList.size()][];
      WorkflowRecord record;
      for (int i = 0; i < recordList.size(); i++) {
        record = (WorkflowRecord) recordList.get(i);
        parameterOptions[i] = (new String[] { record.getWorkflowCode(),
            record.getWorkflowCode() });
      }
    } else {
      parameterOptions = new String[0][];
    }
    request.setAttribute("parameterOptions", parameterOptions);

    WorkflowRecord currentWorkflowRecord = null;
    if (!Utility.isEmpty(parameterform.getParameterListValue())) {
      String[] parameterSectionArray = TextUtility.splitString(parameterform
          .getParameterListValue(), "|");
      if (!Utility.isEmpty(parameterSectionArray)) {
        String[] parameterPropertyArray = TextUtility.splitString(
            parameterSectionArray[0], ":");
        if (parameterPropertyArray.length >= 2) {
          Object workflowRecord = recordDAO
              .getEffectiveWorkflowRecordByWorkflowCode(parameterPropertyArray[1]);
          if (!Utility.isEmpty(workflowRecord)) {
            currentWorkflowRecord = (WorkflowRecord) workflowRecord;
          }
        }
      }
    }
    boolean defaultRecordFromRecordList = (currentWorkflowRecord == null && !Utility
        .isEmpty(recordList));
    if (defaultRecordFromRecordList) {
      currentWorkflowRecord = (WorkflowRecord) recordList.get(0);
    }
    if (currentWorkflowRecord == null) {
      currentWorkflowRecord = new WorkflowRecord();
    }

    request.setAttribute("currentWorkflowRecord", currentWorkflowRecord);
    // BUG:EIP-110 brook.huang_add_end

    WorkflowAction wfAction = (WorkflowAction) actionDAO.getObjectByID(Integer
        .valueOf((parameterform.getActionType()).substring(1)));

    WorkflowProcessor processor;
    try {
      processor = (WorkflowProcessor) Class.forName(
          wfAction.getActionClassName()).newInstance();
      parameterform.setParameterNames(processor.getParameterNames());
    } catch (InstantiationException e) {
      log.error(e);
    } catch (IllegalAccessException e) {
      log.error(e);
    } catch (ClassNotFoundException e) {
      log.error(e);
    }

    // String opMode = (parameterform != null) ? parameterform.getOpMode()
    // : request.getParameter("opMode");
    // String navMode = (parameterform != null) ? parameterform.getNavMode()
    // : request.getParameter("navMode");

    return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);

  }

}
